#!/bin/bash
#pMusic - external menu
#GPL - Copyright Sigmund Berglund

export TEXTDOMAIN=pmusic
export OUTPUT_CHARSET=UTF-8
SF='space-expand="false" space-fill="false"'
ST='space-expand="true" space-fill="true"'

. $CONFIG

GENRES=""
while read I; do
	GENRES="$GENRES <menuitem><label>"$I"</label><action>echo '"$I"' > $WORKDIR/smartadd_GENRE</action><action>refresh:SMARTADD_GENRE</action></menuitem>"
done <<< "$(awk -F' ' 'NF{NF-=1};1' <$APPDIR/txt_id3genre)"

S='
<window decorated="false" skip_taskbar_hint="true" resizable="false" border-width="0" '$ST'>
 <eventbox name="rightclick_menu_frame" above-child="false">
  <vbox border-width="1">
   <eventbox>
    <vbox spacing="7" border-width="10">

     <hbox>
      <pixmap '$SF'>
       <height>32</height>
       <input file>'$APPDIR'/themes/'$THEME'/icon_audio_rated2.svg</input>
      </pixmap>
      <text use-markup="true" '$SF'><label>" <b>'$(gettext 'Rating')' </b>"</label></text>
      <text '$ST'><label>""</label></text>
      <text '$SF'><label>"'$(gettext 'min')' "</label></text>
      <spinbutton range-max="999" width-request="47" '$SF'>
       <variable>SMARTADD_RATE</variable>
       <input>echo '$SMARTADD_RATE'</input>
###       <action signal="activate">EXIT:exit</action>
      </spinbutton>
     </hbox>
   
     <hseparator></hseparator>

     <hbox>
      <pixmap '$SF'>
       <height>32</height>
       <input file>'$APPDIR'/themes/'$THEME'/icon_tempo.svg</input>
      </pixmap>
      <text use-markup="true" '$SF'><label>" <b>'$(gettext 'Tempo (bpm)')' </b>"</label></text>
      <text '$ST'><label>""</label></text>
      <text '$SF'><label>"'$(gettext 'min')' "</label></text>
      <spinbutton range-max="999" width-request="47" '$SF'>
       <variable>SMARTADD_BPM_LO</variable>
       <input>if [ $SMARTADD_BPM_LO -gt $SMARTADD_BPM_HI ]; then echo $SMARTADD_BPM_HI; else echo $SMARTADD_BPM_LO; fi</input>
       <action>refresh:SMARTADD_BPM_HI</action>
      </spinbutton>
      <text '$SF'><label>"'$(gettext 'max')' "</label></text>
      <spinbutton range-max="999" width-request="47" '$SF'>
       <variable>SMARTADD_BPM_HI</variable>
       <input>if [ $SMARTADD_BPM_HI -lt $SMARTADD_BPM_LO ]; then echo $SMARTADD_BPM_LO; else echo $SMARTADD_BPM_HI; fi</input>
       <action>refresh:SMARTADD_BPM_LO</action>
      </spinbutton>'
     if ! type soundstretch > /dev/null 2>&1; then S=$S'<sensitive>false</sensitive>'; fi
     S=$S'</hbox>

     <hseparator></hseparator>

     <hbox>
      <pixmap '$SF'>
       <height>32</height>
       <input file>'$APPDIR'/themes/'$THEME'/icon_genre.svg</input>
      </pixmap>
      <text use-markup="true" '$SF'><label>" <b>'$(gettext 'Genre')' </b>"</label></text>
      <text '$ST'><label>""</label></text>
      <entry activates-default="true" secondary-icon-stock="gtk-clear">
       <variable>SMARTADD_GENRE</variable>
       <input file>'$WORKDIR'/smartadd_GENRE</input>
       <action signal="secondary-icon-release">echo "" > '$WORKDIR'/smartadd_GENRE</action>
       <action signal="secondary-icon-release">refresh:SMARTADD_GENRE</action>
      </entry>
      <menubar width-request="30">
       <menu>
        '$GENRES'
        <label>..</label>
       </menu>
      </menubar>
     </hbox>

    </vbox>
   </eventbox>
  </vbox>
 </eventbox>
 <action signal="focus-out-event">EXIT:exit</action>
</window>'
echo "$S" | sed 's/###.*//' > $WORKDIR/xml_menu #I use triple hash (###) for comments. --> as #FF0000 is valid xml-code --> and because double hash is in use by bash: ${VAR##*/} 

I=$IFS; IFS=""
for STATEMENTS in $($GTKDIALOG -f $WORKDIR/xml_menu -G +"$(($PTR_X_ROOT-5))"+"$(($PTR_Y_ROOT-5))"); do
	eval $STATEMENTS
done
IFS=$I

. $APPDIR/func_config -write_config

#update icons in main window
if [ $SMARTADD_RATE = 0 ]; then
	cp -f $APPDIR/themes/$THEME/icon_audio_rated2.svg $WORKDIR/smartadd_rating.svg
else
	cat $APPDIR/themes/$THEME/icon_audio_rated2.svg | sed "s%</svg>%<text font-family=\"Sans\" font-stretch=\"condensed\" font-weight=\"bold\" font-size=\"70\" text-anchor=\"end\" x=\"98\" y=\"93\" style=\"fill:#111;\">${SMARTADD_RATE}</text></svg>%" > $WORKDIR/smartadd_rating.svg
fi

if [ $SMARTADD_BPM_LO = 0 ]; then
	cp -f $APPDIR/themes/$THEME/icon_tempo.svg $WORKDIR/smartadd_tempo.svg
else
	cat $APPDIR/themes/$THEME/icon_tempo.svg | sed "s%</svg>%<text font-family=\"Sans\" font-stretch=\"condensed\" font-weight=\"bold\" font-size=\"60\" text-anchor=\"end\" x=\"100\" y=\"93\" style=\"fill:#111;letter-spacing:-5pt;\">${SMARTADD_BPM_LO}</text></svg>%" > $WORKDIR/smartadd_tempo.svg
fi

if [ ! "$SMARTADD_GENRE" ]; then
	cp -f $APPDIR/themes/$THEME/icon_genre.svg $WORKDIR/smartadd_genre.svg
else
	cat $APPDIR/themes/$THEME/icon_genre.svg | sed "s%</svg>%<text font-family=\"Sans\" font-stretch=\"condensed\" font-weight=\"bold\" font-size=\"60\" text-anchor=\"start\" x=\"0\" y=\"93\" style=\"fill:#111;letter-spacing:-5pt\">${SMARTADD_GENRE:0:3}</text></svg>%" > $WORKDIR/smartadd_genre.svg
fi


